package com.work.game.utils.risk;


import android.content.Context;
import android.os.BatteryManager;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

public class RiskUtils {

    private Context mContext;


    private RiskUtils() {
    }


    public static volatile RiskUtils instance;

    public static synchronized RiskUtils getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (RiskUtils.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new RiskUtils();
                }
            }
        }
        return instance;
    }

    public boolean detectEmulator(Context context) {
        return checkFeaturesByBaseBand();
    }


    /**
     * 特征参数-基带信息
     *
     * @return true 模拟器
     */
    private boolean checkFeaturesByBaseBand() {
        String baseBandVersion = getProperty("gsm.version.baseband");
        if (StrUtil.isEmpty(baseBandVersion)) {
            return true;
        }
        if (baseBandVersion.contains("1.0.0.0")) {
            return true;
        }
        String[] strings = baseBandVersion.split(",");
        if (strings.length != 2) {
            return true;
        }
        return false;
    }

    /**
     * 电池情况
     *
     * @param context
     * @return
     */
    public int checkBatteryManager(Context context) {
        BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        return batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
    }

    /**
     * 系统出厂时间
     * @return
     */
    public String getFactoryDate() {
        String factoryDate = "";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/sys/class/rfkill/rfkill0/device/mfd/core/pm8941-toggles/toggle@0/state"), 1024);
            factoryDate = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return factoryDate;
    }

    private void checkBatteryManager(List<Integer> integers, Context context, int number) {
        BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
        int batteryCurrent = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
        if (number > 10) {
            return;
        }
        integers.add(batteryCurrent);
        try {
            Thread.sleep(150);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        number++;
        checkBatteryManager(integers, context, number);
    }

    private String getProperty(String propName) {
        String property = CommandUtil.getSingleInstance().getProperty(propName);
        return TextUtils.isEmpty(property) ? null : property;
    }
}
